#! /usr/bin/python
"""
Provides methods that can be used to crack vaults secured with random strings.
"""
import sys
sys.path.insert(0, "..")
import time
from Vault import Vault    

def crack(vault):
    """
    Attempts to break into a vault using brute force
    Times the attempt and returns the guessed value and time taken
    
    Usage: (guess, data, time) = crack(number)
    """
    tick = time.time()
    
    int = 0x0
    guess = str(int).zfill(2).decode("hex")
    data = None
    while (data == None):
        try:
            data = vault.open(guess)
        except:
            int = int + 0x1
            guess = str(int)
            if (len(guess) % 2 == 1):
                guess = guess.zfill(len(guess)+1)
            guess = guess.decode("hex")
            
    tock = time.time()
    return (guess, data, (tock - tick))
    
if __name__ == "__main__":
    v = Vault("abc", "This is super secret information")
    (password, data, cracktime) = crack(v)
    print "I got it! Your key was %s" % password
    print "The vault contained:"
    print "    %s" % data
    print "It took %f seconds to break" % cracktime
